<?php
namespace App\DBs;

use App\DBs\BaseDB;
use Illuminate\Support\Facades\DB;

class ProjectDB extends BaseDB
{
    /**
     * 项目
     * name 项目名称
     * admin 管理员ID
     * detail 初始化需求
     */

    protected $table = 'project';

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 数据列表
     */
    public function getIndex()
    {
        $models = DB::table($this->table)->orderBy('id','desc')->get();
        $dataArr = array();
        if (!count($models)) { return $dataArr; }
        foreach ($models as $model) {
            $createTime = $model->created_at ? date('Y-m-d',$model->created_at) : '未定义';
            $dataArr[] = [
                'proId' => $model->id,
                'proName' => $model->name,
                'proDetail' => $model->detail,
                'createTime' => $createTime,
            ];
        }
        return $dataArr;
    }

    /**
     * 数据详情
     */
    public function getOne($proId)
    {
        $model = DB::table($this->table)->where('id',$proId)
            ->orderBy('id','desc')->get();
        $dataArr = array();
        if (!count($model)) { return $dataArr; }
        $createTime = $model->created_at ? date('Y-m-d',$model->created_at) : '未定义';
        $dataArr = [
            'proId' => $model->id,
            'proName' => $model->name,
            'proDetail' => $model->detail,
            'createTime' => $createTime,
            ];
        return $dataArr;
    }

    /**
     * 新增
     */
    public function setAdd($admin,$name)
    {
        $model = DB::table($this->table)->where('name',$name)->first();
        if ($model) {
            return array('code'=>-2, 'msg'=>'项目已存在');
        }
        //开启事务
        DB::beginTransaction();
        try {
            $lastId = DB::table($this->table)->insertGetId([
                'name' => $name,
                'admin' => $admin,
                'detail' => '',
                'created_at' => time(),
            ]);
            $dataArr = array(
                'proId' => $lastId,
            );
            //提交事务
            DB::commit();
            return array('code'=>0, 'msg'=>'操作成功', 'data'=>$dataArr);
        } catch (\Exception $e) {
            //事务回滚
            DB::rollBack();
            return array('code'=>-1, 'msg'=>'操作失败：'.$e);
        }
    }

    /**
     * 更新需求
     */
    public function setDetail($proId,$detail)
    {
        $model = DB::table($this->table)->where('id',$proId)->first();
        if (!$model) {
            return array('code'=>-2, 'msg'=>'项目不存在');
        }
        //开启事务
        DB::beginTransaction();
        try {
            DB::table($this->table)->where('id',$proId)
                ->update([
                    'detail' => $detail,
                    'updated_at' => time(),
                ]);
            $dataArr = array(
            );
            //提交事务
            DB::commit();
            return array('code'=>0, 'msg'=>'操作成功', 'data'=>$dataArr);
        } catch (\Exception $e) {
            //事务回滚
            DB::rollBack();
            return array('code'=>-1, 'msg'=>'操作失败：'.$e);
        }
    }
}